<?php
class Test_CV_Collection extends UnitTestCase{
	function __construct(){
		setlocale(LC_ALL, 'de_DE.UTF-8', 'de_DE@euro', 'de_DE', 'de', 'ge',"German","Germany", 'deu_deu');
	}
	function test_diff(){
		$collection = new CV_Collection( array(1,2,3) );
		$this->assertIdentical( array_diff(array(1,2,3),array(2)), $collection->diff(array(2))->getArrayCopy() );
	}
	function test_comprehend(){
		$x = new CV_Collection();
		$x[] = array( 'test' => 5, 'test2' => 0 );
		$x[] = array( 'lala' => 7, 'test' => 4, 'test2' => 1 );
		$this->assertIdentical( array(5,4), $x->comprehend( '$v["test"]' )->getArrayCopy() );

	
		$this->assertIdentical( array(5,2 => 4), $x->comprehend( '$v["test2"] * 2 => $v["test"]' )->getArrayCopy() );
	}
	function test_where(){
		$collection = new CV_Collection( array(
			array( 'name' => 'Frank' ),
			array( 'name' => 'Peter' ),
			array( 'name' => 'Joachim' ),
		));
		$this->assertIdentical(
			array( array('name' => 'Peter') ),
			$collection->where( '$name === "Peter"' )->values()->getArrayCopy()
		);
		$this->assertIdentical(
			array('name' => 'Peter'),
			$collection->where( '$v["name"] === $name', array('name'=>'Peter') )->first()
		);
	}
	function test_lc(){
		$collection = new CV_Collection( array(
			array( 'name' => 'Frank' ),
			array( 'name' => 'Peter' ),
			array( 'name' => 'Joachim' ),
		));
		$this->assertIdentical(
			array( array('name' => 'Peter') ),
			$collection->lc( 'array("name" => $i["name"]) for $i in $this if $i["name"]==="Peter"' )->getArrayCopy()
			);
	}
	function test_multisort(){
		$collection = new CV_Collection( array(
			array( 'ID' => 1, 'name' => 'Sz' ),
			array( 'ID' => 2, 'name' => 'Saar' ),
			array( 'ID' => 3, 'name' => 'Sud' ),
		));
		$expected = array(
			array( 'ID' => 2, 'name' => 'Saar' ),
			array( 'ID' => 3, 'name' => 'Sud' ),
			array( 'ID' => 1, 'name' => 'Sz' ),
		);
		$this->assertIdentical(
			$expected,
			$collection->multisort( $collection->column('name'), SORT_ASC )->getArrayCopy()
		);
		
		$collection = new CV_Collection( array(
			array( 'ID' => 3, 'lastname' => 'Schmitz', 'firstname' => 'Bob' ),
			array( 'ID' => 4, 'lastname' => 'Schmitz', 'firstname' => 'Eileen' ),
			array( 'ID' => 1, 'lastname' => 'Vogt', 'firstname' => 'Chris' ),
			array( 'ID' => 2, 'lastname' => 'Vogt', 'firstname' => 'Alice' ),
		));
		$expected = array(
			array( 'ID' => 2, 'lastname' => 'Vogt', 'firstname' => 'Alice' ),
			array( 'ID' => 1, 'lastname' => 'Vogt', 'firstname' => 'Chris' ),
			array( 'ID' => 3, 'lastname' => 'Schmitz', 'firstname' => 'Bob' ),
			array( 'ID' => 4, 'lastname' => 'Schmitz', 'firstname' => 'Eileen' ),
		);
		$this->assertIdentical(
			$expected,
			$collection->multisort( $collection->column('lastname'), SORT_DESC, $collection->column('firstname'), SORT_ASC )->getArrayCopy()
		);
	}
	function test_columnsort(){
		$collection = new CV_Collection( array(
			array( 'ID' => 1, 'lastname' => 'Vogt', 'firstname' => 'Chris' ),
			array( 'ID' => 2, 'lastname' => 'Vogt', 'firstname' => 'Alice' ),
			array( 'ID' => 3, 'lastname' => 'Schmitz', 'firstname' => 'Bob' ),
			array( 'ID' => 10, 'lastname' => 'Schmitz', 'firstname' => 'Eileen' ),
		));
		$expected = array(
			array( 'ID' => 3, 'lastname' => 'Schmitz', 'firstname' => 'Bob' ),
			array( 'ID' => 10, 'lastname' => 'Schmitz', 'firstname' => 'Eileen' ),
			array( 'ID' => 2, 'lastname' => 'Vogt', 'firstname' => 'Alice' ),
			array( 'ID' => 1, 'lastname' => 'Vogt', 'firstname' => 'Chris' ),
		);
		$this->assertIdentical(
			$expected,
			$collection->columnsort( 'lastname', 'firstname' )->values()->getArrayCopy()
		);
		$this->assertIdentical(
			array( 1,2,3,10 ),
			$collection->columnsort( 'ID' )->column('ID')->values()->getArrayCopy()
		);
		$this->assertIdentical(
			array( 10,3,2,1 ),
			$collection->columnsort( 'ID', SORT_DESC )->column('ID')->values()->getArrayCopy()
		);
		$this->assertIdentical(
			array( 1,2,3,10 ),
			$collection->columnsort( 'ID',SORT_ASC )->column('ID')->values()->getArrayCopy()
		);
		$this->assertIdentical(
			array( 3,2,10,1 ),
			$collection->columnsort( 'ID',SORT_DESC,SORT_STRING )->column('ID')->values()->getArrayCopy()
		);
		$this->assertIdentical(
			array( 3,2,10,1 ),
			$collection->columnsort( 'ID',SORT_STRING,SORT_DESC )->column('ID')->values()->getArrayCopy()
		);
	}
	function test_reduce_(){
		$c = new CV_Collection( array(65,2,56,84,2,3,2,4,5) );
		$this->assertIdentical(
			84,
			$c->reduce_('max')
		);
		$c = new CV_Collection( array(65,2,56,84,2,3,2,4,5) );
		$this->assertIdentical(
			0,
			$c->reduce_('min',0)
		);
		$c = new CV_Collection( array(
				array(1,2,3),
				array(3,1,4),
				array(8,5,1),
		));
		$this->assertIdentical(
			array(1,2,3,3,1,4,8,5,1),
			$c->reduce_('array_merge')
		);
	}
	function test_group_by(){
		$c = new CV_Collection( array(
			array( 'test' => 'asdf', 'test2' => 1 ),
			array( 'test' => 'asdf', 'test2' => 2 ),
		));
		$this->assertIdentical(
			array(
				'asdf' => $c->getArrayCopy()
			),
			CV_Collection::unwrap_recursive( $c->group_by('test') )
		);
	}
	function test_mreduce_(){
		$c = new CV_Collection( array(
			new CV_Collection( array(1,2,3) ),
			new CV_Collection( array(3,1,4) ),
			new CV_Collection( array(8,5,1) ),
		));
		$this->assertIdentical(
			array(1,2,3,3,1,4,8,5,1),
			$c->mreduce_('merge')->getArrayCopy()
		);
	}
	function test_merge_all(){
		$c = new CV_Collection( array(
			new CV_Collection( array(1,2,3) ),
			new CV_Collection( array(3,1,4) ),
			new CV_Collection( array(8,5,1) ),
		));
		$this->assertIdentical(
			array(1,2,3,3,1,4,8,5,1),
			$c->merge_all()->getArrayCopy()
		);
	}
	function test_Countable(){
		$c = new CV_Collection(array(1,2,3));
		$this->assertIdentical( 3, count($c) );
	}
}